package ec.cd.client.profiler;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.widget.TextView;

public class SensorsManager implements SensorEventListener {
	private static String TAG = "SensorsManager";

	public SensorsManager(Context context, TextView textViewLog) {
		this.textViewLog = textViewLog;
		this.textViewLog.setText("Hi There");
		sensorManager = (SensorManager) context
				.getSystemService(Context.SENSOR_SERVICE);
		gyroscope = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
		accelerometer = sensorManager
				.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
	}

	protected void startListening(int sensorType, int samplingRate) {
		// textViewLog.setText("Sensor Listening Started.");
		if (sensorType == 0) {
			sensorManager.registerListener(this, gyroscope, samplingRate);
		}
		if (sensorType == 1) {
			sensorManager.registerListener(this, accelerometer, samplingRate);
		}
	}

	protected void stopListening() {
		// textViewLog.append("Sensor Listening Stopped.");
		sensorManager.unregisterListener(this, accelerometer);

		sensorManager.unregisterListener(this, gyroscope);
	}

	// srm: Sensor running mode
	// scs: SensorConfig's, can be null if srm = Calibration
	// Disable all sensors and enable sensors specified in the array
	// Undefined behaviour if scs is invalid

	public void disableAllSensors() {
		stopListening();
	}

	public void onAccuracyChanged(Sensor sensor, int accuracy) {
	}

	public void onSensorChanged(SensorEvent event) {
		// textViewLog.append("Sensor Event received: "
		// +event.sensor.getType());

	}

	private SensorManager sensorManager;
	private Sensor gyroscope;
	private Sensor accelerometer;
	private TextView textViewLog;

}